package androcr.client.mobile.display.cameraviewer;

import java.io.IOException;


import androcr.client.mobile.R;
import androcr.client.mobile.exceptions.MyJSONException;
import androcr.client.mobile.services.cameraviewer.CameraPresenter;
import androcr.client.mobile.services.cameraviewer.ICameraPresenter;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

public class CameraActivity extends Activity implements ICameraActivity {
	private static final String TAG = "CameraActivity";
	
	private ICameraPresenter cameraPresenter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		cameraPresenter = new CameraPresenter(this);

		((FrameLayout) findViewById(R.id.preview)).addView(cameraPresenter.getPreview());


		Button btnTakePicture = (Button) findViewById(R.id.button1);
		btnTakePicture.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Log.i(TAG, "onClick called - take picture");
				cameraPresenter.autoFocusAndTakePicture();
			}
		});

	}

	@Override
	protected void onResume() {
		super.onResume();
		try {
			Log.i(TAG, "camerapres " + cameraPresenter);
			cameraPresenter.resumeCamera();
		} catch (MyJSONException e) {
			cameraPresenter.showAlert(e.getMessage());
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		// it's very important to release it when the activity is paused.
		cameraPresenter.releaseCamera();
	}
}
